public class LoanCalculator {

    public static double calculateEqualPrincipalInterest(double a, int m, double annualInterestRate)
    {
        double i = annualInterestRate / 12 / 100;
        return ((m + 1) * a * i) / 2;
    }

    public static double calculateEqualInstallmentInterest(double a, int m, double annualInterestRate)
    {
        double i = annualInterestRate / 12 / 100; 
        double power = Math.pow(1 + i, m);
        return (m * a * i * power) / (power - 1) - a;
    }

    public static void main(String[] args) {
        // 贷款总额
        double loanAmount = 100;
        // 贷款时长
        int loanTerm = 240;
        // 年利率（百分比）
        double annualInterestRate = 4.5;
        // 计算
        double equalPrincipalInterest = calculateEqualPrincipalInterest(loanAmount, loanTerm, annualInterestRate);
        System.out.println("等额本金还款法的总利息: " + equalPrincipalInterest + " 万元");
        double equalInstallmentInterest = calculateEqualInstallmentInterest(loanAmount, loanTerm, annualInterestRate);
        System.out.println("等额本息还款法的总利息: " + equalInstallmentInterest + " 万元");
    }
}